<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2018 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 千佰特 <http://www.qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_guestbook\controller; //Demo插件英文名，改成你的插件英文就行了
use cmf\controller\PluginBaseController;
use think\Db;
use think\Validate;

class IndexController extends PluginBaseController
{

    /**
     * 提交留言信息
     */
    function post()
    {

        $validate = new Validate([
            'name'      => 'require',
            'email' => 'require|email',
            //'tel' => 'require',
            'content' => 'require',
            'vcode' => 'require',
        ]);
        $validate->message([
            'name'            => 'Please enter your name.',
            'email.require'      => 'Please enter your Email.',
            'email.email'        => 'Email format is not correct.',
            //'tel.require' => 'Please enter your phone.',
            'content.require' => 'Please enter your message.',
            'vcode.require' => 'Please enter verification code.',
        ]);

        $param = $this->request->param();
        if (!$validate->check($param)) {
            //$this->error($validate->getError());
            //使用iframe做调整
            qbt_js_alert($validate->getError(),'parent.resendpemail();');
        }

        if(!captcha_check($param['vcode'])) qbt_js_alert('Verification code error.','parent.$(".sendp_form .verify_img").click();parent.resendpemail();');

        $param['product_name'] = isset($param['product_name'])?$param['product_name']:'';
        $param['product_url'] = isset($param['product_url'])?$param['product_url']:'';

        $data = [
            'name'=>$param['name'],
            'email'=>$param['email'],
            'tel'=>$param['tel'],
            'content'=>$param['content'],
            'addtime'=>time()
        ];

        Db::name('guestbook')->insert($data);

        $config = cmf_get_site_info();
        if($config['site_admin_email']){
            cmf_send_email($config['site_admin_email'], 'WEBSITE MESSAGE #'.date('YmdHis'),"
            Name: {$param['name']}<br>
            Email: {$param['email']}<br>
            Phone: {$param['tel']}<br>
            Product Name: {$param['product_name']}<br>
            Product Url: {$param['product_url']}<br>
            Message: <br>".nl2br($param['content'])."
            ");
        }

        if(empty($param['product_name'])){
            qbt_js_alert('send successful','parent.resendpemail();parent.$(".sendp_form input[type=text]").val("");parent.$(".sendp_form textarea").val("");parent.$(".sendp_form .verify_img").click();');
        }else{
            qbt_js_alert('send successful','parent.$("a.close-reveal-modal").click();parent.resendpemail();parent.$(".sendp_form input[type=text]").val("");parent.$(".sendp_form textarea").val("");parent.$(".sendp_form .verify_img").click();');
        }

    }

}
